package com.hdax.dm.controller;

import com.hdax.dm.dto.querypersoninfoDto;
import com.hdax.dm.dto.ticketbuyerlistDto;
import com.hdax.dm.service.LinkuserService;
import com.hdax.dm.service.userService;
import com.hdax.dm.utils.request.tokenutil;
import com.hdax.dm.utils.response.CommonResponse;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private LinkuserService linkuserService;

    @Autowired
    private userService userService;

    @PostMapping("p/querypersoninfo")
    public CommonResponse<querypersoninfoDto> querypersoninfo(HttpServletRequest request){
        Claims claims = tokenutil.parserToken(request.getHeader("token"));
        Integer id = Integer.parseInt(claims.getId()) ;
        return userService.querypersoninfo(id);
    }

    @PostMapping("ticketbuyerlist")
    public CommonResponse<List<ticketbuyerlistDto>> ticketbuyerlist(HttpServletRequest request){
        Claims claims = tokenutil.parserToken(request.getHeader("token"));
        Integer id = Integer.parseInt(claims.getId()) ;
        return linkuserService.linkUser(id);
    }

    @PostMapping("validatebuyerexist")
    public CommonResponse validatebuyerexist(@RequestBody Map<String,Object> params){
        return linkuserService.validatebuyerexist(params);
    }

    @PostMapping("addticketbuyer")
    public CommonResponse<String> addticketbuyer(@RequestBody Map<String,Object> params,HttpServletRequest request){
        Claims claims = tokenutil.parserToken(request.getHeader("token"));
        Long id = Long.parseLong(claims.getId());
        return linkuserService.addticketbuyer(params,id);
    }

    @PostMapping("deleteticketbuyer")
    public CommonResponse<String> deleteticketbuyer(@RequestBody Map<String,Object> params){
        return linkuserService.deleteticketbuyer(params);
    }
}
